from typing import Any, Union

from core.tools.entities.tool_entities import ToolInvokeMessage
from core.tools.tool.builtin_tool import BuiltinTool
import os
import numpy as np
TEST_NAME="语音"


class SpeechTool(BuiltinTool):
    def _invoke(
        self,
        user_id: str,
        tool_parameters: dict[str, Any],
    ) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
        """
        invoke tools
        """
        # cap = cv2.VideoCapture(0)
        # success = cap.isOpened()
        # # 关闭自动白平衡
        # cap.set(cv2.CAP_PROP_AUTO_WB, 0)
        # ret, frame = cap.read()
        # if not ret or not success:
        #     raise ToolInvokeError("Failed to capture image from webcam")
        # cap.release()

        result = []

        # # for image in response.data:
        # _, buffer = cv2.imencode(".jpg", frame)
        # result.append(
        #         self.create_blob_message(
        #             blob=buffer.tobytes(),
        #             meta={"mime_type": "image/png"},
        #         save_as=self.VariableKey.IMAGE.value,
        #     )
        # )
        
        # return result

        os.system(
            ' '.join([
                '/home/bj-01/anaconda3/bin/python',
                '/home/bj-01/dify/tools_api/pop_win.py',
                f'{TEST_NAME}'
            ])
        )
        fake_result=f'{TEST_NAME}测试结果: aa:{np.random.rand()}, bb:{np.random.rand()}, cc:{np.random.rand()}'
        
        # return result
        # return self.create_text_message("测试已完成")
        return self.create_text_message(fake_result)

